package me.android.library.utils.android;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.google.common.base.Strings;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class ToastUtil {
    public void showThrowable(Throwable t) {
        showShort(t.getMessage());
    }

    public void showException(Exception e) {
        showShort(e.getMessage());
    }

    public void showShort(int resId) {
        show(resId, Toast.LENGTH_SHORT);
    }

    public void showShort(String msg) {
        show(msg, Toast.LENGTH_SHORT);
    }

    public void showLong(int resId) {
        show(resId, Toast.LENGTH_LONG);
    }

    public void showLong(String msg) {
        show(msg, Toast.LENGTH_LONG);
    }

    public void show(int resId, int duration) {
        String msg = ContextUtil.getAppContext().getString(resId);
        show(msg, duration);
    }

    public void show(final String msg, final int duration) {
        if (Strings.isNullOrEmpty(msg)) {
            return;
        }

        Handler h = new Handler(Looper.getMainLooper());
        h.post(() -> Toast.makeText(ContextUtil.getAppContext(), msg, duration).show());
    }

}
